package com.huishu.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.commons.utlis.PasswordUtil;
import com.huishu.user.pojo.dto.AdminUserDTO;
import com.huishu.user.pojo.dto.PageListDTO;
import com.huishu.commons.pojo.model.AdminUserDO;
import com.huishu.user.pojo.model.UserRoleDO;
import com.huishu.user.service.AdminUserService;
import com.huishu.user.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author generator@ljj
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/admin-user")
@Slf4j
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 管理员登录
     *
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result adminLogin(@RequestBody AdminUserDO adminLoginDTO) {
        log.info("管理员登录 adminLoginDTO:{}", JSONObject.toJSONString(adminLoginDTO));
        return adminUserService.adminLogin(adminLoginDTO);
    }


    @PostMapping("/pageList")
    public Result pageList(@RequestBody PageListDTO pageListDTO) {
        return adminUserService.pageList(pageListDTO);
    }

    @PostMapping("/add")
    public Result add(@RequestBody AdminUserDTO adminUserDTO) {
        AdminUserDO adminUserDO = BeanUtils.copyProperties(adminUserDTO, AdminUserDO.class);
        adminUserDO.setPassword(PasswordUtil.generate(adminUserDTO.getPassword()));
        adminUserService.save(adminUserDO);
        List<UserRoleDO> collect = adminUserDTO.getRoleIds().stream().map(id -> {
            UserRoleDO userRoleDO = new UserRoleDO();
            userRoleDO.setUserId(adminUserDO.getId());
            userRoleDO.setRoleId(id);
            return userRoleDO;
        }).collect(Collectors.toList());
        userRoleService.saveBatch(collect);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody AdminUserDTO adminUserDTO) {
        AdminUserDO adminUserDO = BeanUtils.copyProperties(adminUserDTO, AdminUserDO.class);
        adminUserService.updateById(adminUserDO);
        LambdaQueryWrapper<UserRoleDO> queryWrapper = Wrappers.lambdaQuery(UserRoleDO.class)
                .eq(UserRoleDO::getUserId, adminUserDO.getId());
        userRoleService.remove(queryWrapper);
        List<UserRoleDO> collect = adminUserDTO.getRoleIds().stream().map(id -> {
            UserRoleDO userRoleDO = new UserRoleDO();
            userRoleDO.setUserId(adminUserDO.getId());
            userRoleDO.setRoleId(id);
            return userRoleDO;
        }).collect(Collectors.toList());
        userRoleService.saveBatch(collect);
        return Result.success();
    }

    @GetMapping("/enable/{id}/{enable}")
    public Result enable(@PathVariable Long id, @PathVariable int enable) {
        LambdaUpdateWrapper<AdminUserDO> uw = Wrappers.lambdaUpdate(AdminUserDO.class)
                .set(AdminUserDO::getIsEnable, enable)
                .eq(AdminUserDO::getId, id);
        boolean update = adminUserService.update(uw);
        return update ? Result.success() : Result.fail();
    }

    @PostMapping("/update-password")
    public Result enable(@RequestBody AdminUserDO adminUserDO) {
        LambdaUpdateWrapper<AdminUserDO> uw = Wrappers.lambdaUpdate(AdminUserDO.class)
                .set(AdminUserDO::getPassword, PasswordUtil.generate(adminUserDO.getPassword()))
                .eq(AdminUserDO::getId, adminUserDO.getId());
        boolean update = adminUserService.update(uw);
        return update ? Result.success() : Result.fail();
    }


    @GetMapping("/getAdminUserInfo/{adminUid}")
    public Result<AdminUserDO> getAdminUserInfo(@PathVariable Long adminUid) {
        return Result.success(adminUserService.getById(adminUid));
    }
}
